package com.pureut.production.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.LineWarehouse;
import com.pureut.production.domain.dto.LineWarehouseDto;
import com.pureut.production.domain.dto.WorkshopDto;
import com.pureut.production.domain.dto.storage.LineWarehouseChildrenDto;
import com.pureut.production.domain.vo.LineWarehouseVo;
import com.pureut.production.mapper.LineStockMapper;
import com.pureut.production.mapper.LineWarehouseMapper;
import com.pureut.production.service.LineWarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: CM
 * @date: 2023/2/6  15:33
 */

@Service
public class LineWarehouseServiceImpl extends ServiceImpl<LineWarehouseMapper, LineWarehouse> implements LineWarehouseService {

    @Resource
    private LineWarehouseMapper lineWarehouseMapper;

    @Resource
    private LineStockMapper lineStockMapper;


    /**
     * 线边仓库存列表
     *
     * @return
     */
    @Override
    public List<LineWarehouseDto> getList(LineWarehouseVo lineWarehouseVo) {
        //获取车间编码
        List<LineWarehouseDto> lineWarehouseList = lineWarehouseMapper.selectWorkshopCode(lineWarehouseVo);

        for (LineWarehouseDto lineWarehouse : lineWarehouseList) {

            //设置车间编码
            lineWarehouse.setId(lineWarehouse.getWorkshopCode());
            //设置车间编码
            lineWarehouse.setWarehouseCode(lineWarehouse.getWorkshopCode());
            //设置车间名称
            lineWarehouse.setWarehouseName(lineWarehouse.getWorkshopName());
            //根据车间编码，获取线边仓信息
            List<LineWarehouseChildrenDto> lineWarehouse1 = lineWarehouseMapper.selectLineWareInfo(lineWarehouse.getWorkshopCode());

            lineWarehouse.setChildren(lineWarehouse1);
        }
        return lineWarehouseList;
    }

    /**
     * 新增线边仓车间建模
     *
     * @param lineWarehouseVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWarehouse(LineWarehouseVo lineWarehouseVo) {
        LineWarehouse lineWarehouse = new LineWarehouse();

        lineWarehouse.setCreateTime(new Date());
        BeanUtils.copyProperties(lineWarehouseVo, lineWarehouse);
        lineWarehouse.setWorkshopCode(lineWarehouseVo.getWorkshop());
        lineWarehouse.setLineCode(lineWarehouseVo.getAddType());
        lineWarehouse.setDeptId(SecurityUtils.getDeptId());

        //查询编码是否存在
        LineWarehouse lineWarehouse1 = lineWarehouseMapper.selectOne(new QueryWrapper<LineWarehouse>().lambda().eq(LineWarehouse::getWarehouseCode, lineWarehouse.getWarehouseCode()));
        if (lineWarehouse1 != null) {
            throw new GlobalException("编码重复");
        }
        return save(lineWarehouse);
    }

    /**
     * 线边仓库列表查询
     *
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<LineWarehouseDto> getlineList(LineWarehouseVo lineWarehouseVo) {
        List<LineWarehouseDto> lineWarehouseDtos = lineWarehouseMapper.getlineList(lineWarehouseVo);
        //线边列表查询
        return lineWarehouseDtos;
    }

    /**
     * 线边仓查看
     * @param id
     * @return
     */
    @Override
    public LineWarehouseDto getView(Long id) {
        return lineWarehouseMapper.getViewId(id);
    }

    /**
     * 删除仓库
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWarehouse(Long id) {

        //查询线边仓库中是否还有物料，否则不让删除
        //long count = lineStockMapper.selectMaterial(id);

        // if (count>0){
        //     throw new GlobalException("改线边仓库下还有物料存在不能删除");
        // }

        return removeById(id);
        //return removeById(id);
    }

    /**
     * 修改仓库
     * @param lineWarehouseVo
     * @return
     */
    @Override
    public boolean updateWarehouse(LineWarehouseVo lineWarehouseVo) {
        LineWarehouse lineWarehouse = lineWarehouseMapper.selectById(lineWarehouseVo.getId());
        lineWarehouse.setWarehouseName(lineWarehouseVo.getWarehouseName());
        lineWarehouse.setWorkshopCode(lineWarehouseVo.getWorkshop());
        lineWarehouse.setLineCode(lineWarehouseVo.getAddType());
        lineWarehouse.setCapacity(lineWarehouseVo.getCapacity());
        lineWarehouse.setVolume(lineWarehouseVo.getVolume());
        lineWarehouse.setRemark(lineWarehouseVo.getRemark());
        return updateById(lineWarehouse);
    }

    /**
     * 车间列表
     *
     * @return
     */
    @Override
    public List<WorkshopDto> getWorkshop() {

        return lineWarehouseMapper.getWorkshop();
    }
}
